﻿using Nancy;
using Nancy.Responses.Negotiation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YYBlog.Core;
using YYBlog.Core.Entity;
using YYBlog.Core.ViewProjections.Home;

namespace YYBlog.Web.Modules
{
    public class HomeModule : FrontModule
    {
        private readonly ICommandInvokerFactory _commandInvokerFactory;

        public HomeModule(IViewProjectionFactory viewFactory, ICommandInvokerFactory commandInvokerFactory)
            : base(viewFactory)
        {
            _viewFactory = viewFactory;
            _commandInvokerFactory = commandInvokerFactory;

            Get["/"] = p =>
                ReturnHomeAction(new RecentBlogPostsBindingModel() { Page = 1, Take = 10 });

            Get["/page/{page:int}"] = p =>
                ReturnHomeAction(new RecentBlogPostsBindingModel() { Page = p.page, Take = 10 });

            Get["/tag/{Tag}"] = p =>
                ReturnArticlesTaggedBy(new TaggedBlogPostsBindingModel() { Tag = p.tag });

            Get[@"/(?<year>\d{4})/(?<month>0[1-9]|1[0-2])/(?<titleslug>[a-zA-Z0-9_-]+)"] = p =>
                ReturnArticle(new BlogPostDetailsBindingModel { Permalink = p.titleslug });

            Get[@"/(?<year>\d{4})/(?<month>0[1-9]|1[0-2])"] = p =>
            {
                var input = new IntervalBlogPostsBindingModel
                {
                    FromDate = new DateTime(p.year, p.month, 1)
                };

                input.ToDate = input.FromDate.AddMonths(1);

                return ReturnArticles(input);
            }; 
        }

        public dynamic ReturnHomeAction(RecentBlogPostsBindingModel input)
        {
            var model = _viewFactory.Get<RecentBlogPostsBindingModel, RecentBlogPostsViewModel>(input);
            if (model.Posts.Count() == 0)
            {
                if (input.Page > 1)
                    return HttpStatusCode.NotFound;
                else
                    return Response.AsText("尚未发现任何已经发布的文章哦!", "text/html; charset=utf-8");
            }
            if (input.Page == 1)
                ViewBag.Title = "首页";
            else
                ViewBag.Title = "文章列表";

            return View["Index", model];
        }

        public dynamic ReturnArticle(BlogPostDetailsBindingModel input)
        {
            var model = _viewFactory.Get<BlogPostDetailsBindingModel, BlogPostDetailsViewModel>(input);

            if (model == null)
                return HttpStatusCode.NotFound;

            ViewBag.Title = model.BlogPost.Title;

            return View["Details", model];
        }

        public Negotiator ReturnArticles(IntervalBlogPostsBindingModel input)
        {
            var model = _viewFactory.Get<IntervalBlogPostsBindingModel, IntervalBlogPostsViewModel>(input);

            return View["Archive", model];
        }

        public dynamic ReturnArticlesTaggedBy(TaggedBlogPostsBindingModel input)
        {
            var model = _viewFactory.Get<TaggedBlogPostsBindingModel, TaggedBlogPostsViewModel>(input);
            if (model == null)
                return HttpStatusCode.NotFound;

            ViewBag.Title = "标签:" + model.Tag;

            return View["Tagged", model];
        }
    }
}